﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;
using System.Xml;

namespace SqlServerDataTableToExcel
{
    public class AppConfig
    {
   

        /// <summary>
        /// DES 对称加密
        /// </summary>
        public class DESSymmetric
        {
            protected const string _Key = "98K%#PJP"; //加密密钥必须为8位
            protected const string _Iv = "SU2ZHOU_JINGYI@HUI"; //加密位移必须大于8位


            /// <summary>
            /// 加密
            /// </summary>
            /// <param name="sourceString"></param>
            /// <returns></returns>
            public static string Encrypt(string sourceString)
            {
                try
                {
                    byte[] btKey = Encoding.UTF8.GetBytes(_Key);

                    byte[] btIV = Encoding.UTF8.GetBytes(_Iv);

                    DESCryptoServiceProvider des = new DESCryptoServiceProvider();

                    using (MemoryStream ms = new MemoryStream())
                    {
                        byte[] inData = Encoding.UTF8.GetBytes(sourceString);
                        try
                        {
                            using (CryptoStream cs = new CryptoStream(ms, des.CreateEncryptor(btKey, btIV), CryptoStreamMode.Write))
                            {
                                cs.Write(inData, 0, inData.Length);

                                cs.FlushFinalBlock();
                            }

                            return Convert.ToBase64String(ms.ToArray());
                        }
                        catch
                        {
                            return sourceString;
                        }
                    }
                }
                catch
                {
                    return "DES加密出错";
                }
            }


            /// <summary>
            /// 解密
            /// </summary>
            /// <param name="encryptedString"></param>
            /// <returns></returns>
            public static string Decrypt(string encryptedString)
            {
                byte[] btKey = Encoding.UTF8.GetBytes(_Key);

                byte[] btIV = Encoding.UTF8.GetBytes(_Iv);

                DESCryptoServiceProvider des = new DESCryptoServiceProvider();

                using (MemoryStream ms = new MemoryStream())
                {
                    byte[] inData = Convert.FromBase64String(encryptedString);
                    try
                    {
                        using (CryptoStream cs = new CryptoStream(ms, des.CreateDecryptor(btKey, btIV), CryptoStreamMode.Write))
                        {
                            cs.Write(inData, 0, inData.Length);

                            cs.FlushFinalBlock();
                        }

                        return Encoding.UTF8.GetString(ms.ToArray());
                    }
                    catch
                    {
                        return encryptedString;
                    }
                }
            }
        }

    }
}
